<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <!--{include file='comm/header.tpl'}-->
    <!--{include file='comm/comm_cal.tpl'}-->
    <style>.flexigrid{margin:10px;}</style>
    <title><!--{if isset($operFlg) && $operFlg == 2}-->会员促销添加<!--{else}-->会员促销编辑<!--{/if}--></title>
    <!--{include file="comm/icheck.tpl"}-->
</head>
<body>
<div style="width:100%;">

<div class="container">

    <div class="comm_crub">
        <span class="now_crub"><!--{if isset($operFlg) && $operFlg == 2}-->会员促销添加<!--{else}-->会员促销编辑<!--{/if}--></span>
    </div>

    <div class="comm_content">

        <div class="commAttr">
            <form id="sendForm" class="form-horizontal" name="sendForm" onsubmit="return false;">

                <div class="form-group">
                    <label for="title" class="col-sm-2 control-label"><span class="red">*</span> 促销标题：</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control w300" id="title" name="title" placeholder="请输入促销标题【必填项】" value="<!--{$promotion_customer->title|default:''}-->" >
                    </div>
                    <input type="hidden" id="promotionID" name="promotionID" value="<!--{$promotion_customer->promotionID|default:''}-->">
                </div>

                <div class="form-group">
                    <label for="source" class="col-sm-2 control-label"> 活动类型：</label>
                    <div class="col-sm-10">
                        <select class="form-control w150 fl" name="source" id="source" >
                            <option value="" selected>请选择</option>
                            <option value="1" <!--{if isset($promotion_customer) && $promotion_customer->source == 1}--> selected <!--{/if}--> >注册</option>
                            <option value="2" <!--{if isset($promotion_customer) && $promotion_customer->source == 2}--> selected <!--{/if}--> >登陆</option>
                            <option value="4" <!--{if isset($promotion_customer) && $promotion_customer->source == 4}--> selected <!--{/if}--> >建议</option>
                            <option value="5" <!--{if isset($promotion_customer) && $promotion_customer->source == 5}--> selected <!--{/if}--> >问卷调查</option>
                            <option value="6" <!--{if isset($promotion_customer) && $promotion_customer->source == 6}--> selected <!--{/if}--> >完善资料</option>
                            <option value="7" <!--{if isset($promotion_customer) && $promotion_customer->source == 7}--> selected <!--{/if}--> >商品评论</option>
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <div>
                        <label class="col-sm-2 control-label"><span class="red">*</span> 促销时间：</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control w200" id="startDate" name="startDate" readonly="" value="<!--{$promotion_customer->startDate|default:''}-->" >
                        </div>
                        <div class="fl">
                            <label class="col-sm-1 control-label">～</label>
                        </div>
                        <div class="col-sm-3">
                            <input type="text" class="form-control w200" id="endDate" name="endDate" readonly="" value="<!--{$promotion_customer->endDate|default:''}-->" >
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label"> 使用状态：</label>
                    <div class="col-sm-10">
                        <div class="fl" style="margin-right: 10px;">
                            <input type="radio" class="square-radio" id="square-radio-1" name="useFlg" value="1" <!--{if isset($operFlg) && $operFlg == 2 || $promotion_customer->useFlg == 1}-->checked="checked"<!--{/if}-->>
                            <label for="square-radio-1">启用</label>
                        </div>
                        <div class="fl">
                            <input type="radio" class="square-radio" id="square-radio-0" name="useFlg" value="0" <!--{if isset($operFlg) && $operFlg == 3 && $promotion_customer->useFlg == 0}-->checked="checked"<!--{/if}-->>
                            <label for="square-radio-0">禁用</label>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label for="selectorID" class="col-sm-2 control-label"><span class="red">*</span> 促销类型：</label>
                    <div class="col-sm-10">
                        <!--{if $operFlg == 2}-->
                        <select class="form-control w150 fl" name="type" id="type" >
                            <option value="" selected>请选择</option>
                            <option value="1">送积分</option>
                            <option value="2">送优惠券</option>
                        </select>
                        <!--{else}-->
                        <!--{if $promotion_customer->type == 1}-->
                        <font style="font-size: 14px;">送积分</font>
                        <!--{elseif $promotion_customer->type == 2}-->
                        <font style="font-size: 14px;">送优惠券</font>
                        <!--{/if}-->
                        <input type="hidden" name="type" id="type" value="<!--{$promotion_customer->type|default:''}-->">

                        <!--{/if}-->
                    </div>
                </div>

                <div class="form-group" id="pointsTr" <!--{if $operFlg == 3 && $promotion_customer->type == 1}-->style="" <!--{else}--> style="display: none;" <!--{/if}-->>
                    <label for="points" class="col-sm-2 control-label"><span class="red">*</span> 积分：</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control w300" id="points" name="points" placeholder="请输入积分【必填项】" value="<!--{$promotion_customer->points|default:''}-->">
                    </div>
                </div>

                <div class="form-group" id="couponDiv" <!--{if $operFlg == 3 && $promotion_customer->type == 2}--> style="display: block;" <!--{else}--> style="display: none;" <!--{/if}--> >
                    <div>
                        <label class="col-sm-2 control-label"><span class="red">*</span> 优惠券：</label>
                        <div class="col-sm-10">
                            <table id="couponTable">
                                <!--{assign var="seq" value=1}-->
                                <!--{if isset($CustomerCouponInfo)}-->
                                <!--{foreach from=$CustomerCouponInfo item=couponInfo}-->
                                <tr id="addCouponTr_<!--{$seq}-->" style="height:35px;" >
                                    <td class="td_input">
                                        <div class="col-sm-3" style="margin:0 -15px" >
                                            <input type="text" class="form-control" id="couponNum_<!--{$seq}-->" name="couponNum_<!--{$seq}-->" value="<!--{$couponInfo->couponNum|default:''}-->" maxlength="7" autocomplete="off"  style="ime-mode:active;" >
                                        </div>
                                        <div class="fl">
                                            <label class="col-sm-1 control-label">张</label>
                                        </div>
                                        <select class="form-control fl" style="width: 250px;" name="couponCode_<!--{$seq}-->" id="couponCode_<!--{$seq}-->" >
                                            <!--{if isset($accountCoupon)}-->
                                            <!--{foreach from=$accountCoupon item=c_l}-->
                                            <option value="<!--{$c_l->couponID}-->" <!--{if $c_l->couponID == $couponInfo->couponID}--> selected <!--{/if}--> ><!--{$c_l->couponName}--></option>
                                            <!--{/foreach}-->
                                            <!--{/if}-->
                                        </select>
                                        <!--{if $seq == 1}-->
                                        <input  type="button" style="margin:0 20px;" onclick="promotion.coupoonList();" class="btn btn-success" value="添加">
                                        <!--{else}-->
                                        <input type="button" style="margin:0 20px;" class="btn btn-success" onclick="promotion.delCoupon(<!--{$seq}-->)" value="移除">
                                        <!--{/if}-->

                                        <!--{assign var="seq" value=$seq+1}-->

                                    </td>
                                </tr>
                                <!--{/foreach}-->

                                <!--{else}-->
                                <tr id="addCouponTr_1" style="height:35px;">
                                    <td class="td_input">
                                        <div class="col-sm-3" style="margin:0 -15px" >
                                            <input type="text" class="form-control" id="couponNum_1" name="couponNum_1" maxlength="7" autocomplete="off"  style="ime-mode:active;" >
                                        </div>
                                        <div class="fl">
                                            <label class="col-sm-1 control-label">张</label>
                                        </div>
                                        <select class="form-control fl" style="width: 250px;" name="couponCode_1" id="couponCode_1" >
                                            <!--{if isset($accountCoupon)}-->
                                            <!--{foreach from=$accountCoupon item=c_l}-->
                                            <option value="<!--{$c_l->couponID}-->"><!--{$c_l->couponName}--></option>
                                            <!--{/foreach}-->
                                            <!--{/if}-->
                                        </select>
                                        <input type="button" style="margin:0 20px;"  onclick="promotion.coupoonList();" class="btn btn-success" value="添加">
                                    </td>
                                </tr>
                                <!--{/if}-->
                            </table>
                        </div>
                    </div>
                </div>

            </form>
        </div>

    </div>
</div>

<div class="popup-footer">
    <div class="rectbox tac"  style="margin: 20px;">
        <input type="hidden" id ="dataCache" name="dataCache" value="">
        <input type="button" class="btn btn-success" onclick="promotion.check();" value="<!--{if $operFlg == 2}-->会员促销添加<!--{else}-->会员促销编辑<!--{/if}-->" />&nbsp;&nbsp;&nbsp;
        <input type="button" class="btn btn-default" onclick="E.closeWindows();" value="关闭" />
    </div>
</div>

</div>

<script>

    $(window).ready(function() {

        $('.square-radio').iCheck({
            checkboxClass: 'icheckbox_square-blue',
            radioClass: 'iradio_square-blue',
            increaseArea: '20%' // optional
        });

    });


    $('#startDate').datetimepicker({timeFormat:'HH:mm:ss',showHour: true,showMinute: true,showSecond: true,});
    $('#endDate').datetimepicker({timeFormat:'HH:mm:ss',showHour: true,showMinute: true,showSecond: true,hour: 23,minute: 59,second:59,});
    //添加或编辑的标志
    var operFlg = '<!--{$operFlg}-->';

    //记录优惠券错误信息行
    var errorArr = new Array();

    //促销类型下拉框变更事件
    $("#type").change(function(k,v){

        var value = $(this).val();

        if( value == '1' ){

            $("#pointsTr").show();
            $("#couponDiv").hide();

        }else if ( value == '2' ){

            $("#couponDiv").show();
            $("#pointsTr").hide();

        }else{

            $("#pointsTr").hide();
            $("#couponDiv").hide();

        }

    })


var COUPONFLG = 100;	//添加优惠券行标志
var promotion = {

    //添加优惠券输入行
    coupoonList : function(){
        select_str = $('#couponCode_1').html();

          var tr_str = '<tr id=addCouponTr_'+COUPONFLG+' style="height:35px;">';
                tr_str += '<td class="td_input">';
                tr_str += '<div class="col-sm-3" style="margin:0 -15px" >';
                tr_str += '<input id=couponNum_'+COUPONFLG+' type="text" class="form-control" name=couponNum_'+COUPONFLG+' maxlength="7" autocomplete="off"  style="ime-mode:active;" >';
                tr_str += '</div>';
                tr_str += '<div class="fl">';
                tr_str += '<label class="col-sm-1 control-label">张</label>';
                tr_str += '</div>';
                tr_str += '<select name=couponCode_'+COUPONFLG+' id=couponCode_'+COUPONFLG+' class="form-control fl" style="width: 250px;" >';
                tr_str += select_str;
                tr_str += '</select>';
                tr_str += ' <input type="button" class="btn btn-default" style="margin:0 20px;" onclick=promotion.delCoupon('+COUPONFLG+') value="移除">';
                tr_str += '</td>';
                tr_str += '</tr>';

        COUPONFLG++
        $('#couponTable').append(tr_str);
    },

    //删除添加优惠券的行
    delCoupon : function(flg){
        $('#addCouponTr_'+flg).remove();
    },

    //检查数据
    check: function() {

        this.dt = E.getFormValues("sendForm") ;

        var error_msg = "";

        if (this.dt.title == "")
            error_msg += "促销标题不能为空<br />";

        if( this.dt.source == "" )
            error_msg += "请选择活动类型<br />";

        if( this.dt.startDate == '' ){
            error_msg += "请输入促销开始时间<br />";
        }else{
            if (!E.isDate(this.dt.startDate))
                error_msg += "促销开始时间格式不正确<br />";
        }

        if( this.dt.endDate == '' ){
            error_msg += "请输入促销结束时间<br />";
        }else{
            if (!E.isDate(this.dt.endDate))
                error_msg += "促销结束时间格式不正确<br />";
        }

        if(this.dt.startDate > this.dt.endDate)
            error_msg += "促销开始时间不能大于结束时间<br />";

        if( this.dt.type == '' )
            error_msg += "请选择促销类型<br />";

        if( this.dt.type == 1 ){	//送积分

            if( this.dt.points == "" ){
                error_msg += "请输入积分<br />";
            }else{
                if(!E.isInt( this.dt.points ))
                    error_msg += "积分必须为正整数<br />" ;
            }

        }else if( this.dt.type == 2 ){	//送优惠券

            //优惠券张数
            $.each($('[name*=couponNum_]'), function(k, v){
                var couponNum = $.trim($(v).val());
                var id        = $(this).attr("id");
                if(!E.isInt(couponNum) || couponNum == 0){
                    errorArr.push(id);
                }else{
                    $("#" + id).css("color","black");
                }
            })

            if( errorArr.length > 0 ){

                $.each(errorArr,function(k,v){
                    $("#" + v).css("color","red");
                })

                error_msg += "优惠券张数必须是正整数<br />";

                errorArr.length = 0; //重置错误信息数组，清除里面的记录，以便下次重新记录错误行

            }

            //优惠券编号
            var couponType = [];
            $.each($('[name*=couponCode_]'), function(k, v){
                var couponCode = $.trim($(v).val());
                if(!E.isInt(couponCode) || couponCode == 0){
                    error_msg += "优惠券的编号必须为正整数<br />";
                    return false;
                }
                couponType.push(couponCode);
            })

            //判断数组中是否存在同一元素
            var repeat = promotion.checkRepeat(couponType);
            if(repeat)
                error_msg += "优惠券类型不能重复<br />";

            //添加优惠券行数参数
            var couponObj = [];
            $.each($('[name*=couponNum_]'), function(k, v){
                var numID = $(v).attr('id');
                var numArray = numID.split('_');

                //获取优惠券张数和优惠券编号
                var couponNum = $('#couponNum_'+numArray[1]).val();
                var couponID  = $('#couponCode_'+numArray[1]).val();
                couponObj.push([couponNum, couponID]);
            })
            this.dt['couponLine'] = couponObj;

        }

        if (error_msg != "")
            E.alert(error_msg);
        else
            promotion.add();
    },

    //保存促销信息
    add: function() {
        $.ajax({
            type: "POST",
            url: "/invokingAjax-backend/promotion/promotionCustomer.ajax?operFlg=100&act=edit" ,
            dataType: "JSON",
            data: this.dt,
            success: function( result_obj ) {
                if (result_obj.code == 200) {

                    E.opener("promotion.load");

                    if( operFlg == 2 ){
                        E.alert(result_obj.message, 2 , "E.refresh");
                    }else{
                        E.alert(result_obj.message, 2 , "E.closeWindows");
                    }

                } else {
                    E.alert(result_obj.message);
                }
            }
        });
    },

    //检查数组中是否有重复的元素
    checkRepeat : function(a){
        return /(\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f"+ a.join("\x0f\x0f") +"\x0f");
    }
};

    $('#startDate').datepicker();
    $('#endDate').datepicker();

</script>

</body>
</html>